home *** CD-ROM | disk | FTP | other *** search
/ Aminet 52 / Aminet 52 (2002)(GTI - Schatztruhe)[!][Dec 2002].iso / Aminet / game / think / AmiChess.lha / AmiChess / src / mui_chess.c < prev    next >
C/C++ Source or Header  |  2002-10-31  |  26KB  |  831 lines

  1. #include <clib/alib_protos.h>
  2. #include <clib/dos_protos.h>
  3. #include <clib/intuition_protos.h>
  4. #include <clib/muimaster_protos.h>
  5. #include <libraries/asl.h>
  6. #include <libraries/gadtools.h>
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <stdlib.h>
  10. #include "common.h"
  11.  
  12. void LoadSkin(char *name);
  13.  
  14. extern struct MUI_CustomClass *MUI_Field_Class;
  15. extern char *skin_folder;
  16. extern struct Screen *wbscreen;
  17.  
  18. static Object *mui_menu;
  19.  
  20. #define find(id) ((APTR)DoMethod(mui_menu,MUIM_FindUData,id))
  21.  
  22. struct Data
  23. {
  24. Object *win,*col,*mymove,*think,*board,*lv_white,*lv_black;
  25. Object *field[64];
  26. Object *texta[8],*text1[8];
  27. Object *timewin,*timem,*timet,*times;
  28.  
  29. struct FileRequester *filereq;
  30. };
  31.  
  32. enum
  33. {
  34. ID_MENU_New=1,
  35. ID_MENU_OpenEPD,
  36. ID_MENU_OpenPGN,
  37. ID_MENU_SaveEPD,
  38. ID_MENU_SavePGN,
  39. ID_MENU_Autoplay,
  40. ID_MENU_Supervisor,
  41. ID_MENU_Swap,
  42. ID_MENU_Switch,
  43. ID_MENU_Hint,
  44. ID_MENU_Undo,
  45. ID_MENU_Remove,
  46. ID_MENU_ShowThinking,
  47. ID_MENU_NullMove,
  48. ID_MENU_BookAdd,
  49. ID_MENU_BookOff,
  50. ID_MENU_BookOn,
  51. ID_MENU_BookBest,
  52. ID_MENU_BookWorst,
  53. ID_MENU_BookRandom,
  54. ID_MENU_Depth0,
  55. ID_MENU_Depth1,
  56. ID_MENU_Depth2,
  57. ID_MENU_Depth3,
  58. ID_MENU_Depth4,
  59. ID_MENU_Depth5,
  60. ID_MENU_Depth6,
  61. ID_MENU_Depth7,
  62. ID_MENU_Depth8,
  63. ID_MENU_Depth9,
  64. ID_MENU_Depth10,
  65. ID_MENU_Time,
  66. ID_MENU_ReverseBoard,
  67. ID_MENU_Skin
  68. };
  69.  
  70. struct MUI_CustomClass *MUI_Chess_Class;
  71.  
  72. static struct NewMenu nm[]={
  73. {NM_TITLE,"Project"},
  74. {NM_ITEM,"N\0New game",0,0,0,(APTR)ID_MENU_New},
  75. {NM_ITEM,"Open EPD ...",0,0,0,(APTR)ID_MENU_OpenEPD},
  76. {NM_ITEM,"Open PGN ...",0,0,0,(APTR)ID_MENU_OpenPGN},
  77. {NM_ITEM,"Save EPD ...",0,0,0,(APTR)ID_MENU_SaveEPD},
  78. {NM_ITEM,"Save PGN ...",0,0,0,(APTR)ID_MENU_SavePGN},
  79. {NM_ITEM,NM_BARLABEL},
  80. {NM_ITEM,"Q\0Quit",0,0,0,(APTR)MUIV_Application_ReturnID_Quit},
  81. {NM_TITLE,"Game"},
  82. {NM_ITEM,"A\0Autoplay",0,CHECKIT,0,(APTR)ID_MENU_Autoplay},
  83. {NM_ITEM,"V\0Supervisor",0,CHECKIT,0,(APTR)ID_MENU_Supervisor},
  84. {NM_ITEM,"W\0Swap sides",0,0,0,(APTR)ID_MENU_Swap},
  85. {NM_ITEM,"Switch sides",0,0,0,(APTR)ID_MENU_Switch},
  86. {NM_ITEM,"H\0Hint",0,0,0,(APTR)ID_MENU_Hint},
  87. {NM_ITEM,"U\0Undo",0,0,0,(APTR)ID_MENU_Undo},
  88. {NM_ITEM,"R\0Remove",0,0,0,(APTR)ID_MENU_Remove},
  89. {NM_ITEM,NM_BARLABEL},
  90. {NM_ITEM,"Book"},
  91. {NM_SUB,"Add ...",0,0,0,(APTR)ID_MENU_BookAdd},
  92. {NM_SUB,NM_BARLABEL},
  93. {NM_SUB,"Off",0,CHECKIT,~0x04,(APTR)ID_MENU_BookOff},
  94. {NM_SUB,"On",0,CHECKIT|CHECKED,~0x08,(APTR)ID_MENU_BookOn},
  95. {NM_SUB,"Best",0,CHECKIT,~0x10,(APTR)ID_MENU_BookBest},
  96. {NM_SUB,"Worst",0,CHECKIT,~0x20,(APTR)ID_MENU_BookWorst},
  97. {NM_SUB,"Random",0,CHECKIT,~0x40,(APTR)ID_MENU_BookRandom},
  98. {NM_ITEM,"Search depth"},
  99. {NM_SUB,"Use time settings",0,CHECKIT|CHECKED,~0x01,(APTR)ID_MENU_Depth0},
  100. {NM_SUB," 1",0,CHECKIT,~0x02,(APTR)ID_MENU_Depth1},
  101. {NM_SUB," 2",0,CHECKIT,~0x04,(APTR)ID_MENU_Depth2},
  102. {NM_SUB," 3",0,CHECKIT,~0x08,(APTR)ID_MENU_Depth3},
  103. {NM_SUB," 4",0,CHECKIT,~0x10,(APTR)ID_MENU_Depth4},
  104. {NM_SUB," 5",0,CHECKIT,~0x20,(APTR)ID_MENU_Depth5},
  105. {NM_SUB," 6",0,CHECKIT,~0x40,(APTR)ID_MENU_Depth6},
  106. {NM_SUB," 7",0,CHECKIT,~0x80,(APTR)ID_MENU_Depth7},
  107. {NM_SUB," 8",0,CHECKIT,~0x100,(APTR)ID_MENU_Depth8},
  108. {NM_SUB," 9",0,CHECKIT,~0x200,(APTR)ID_MENU_Depth9},
  109. {NM_SUB,"10",0,CHECKIT,~0x400,(APTR)ID_MENU_Depth10},
  110. {NM_ITEM,"Time settings",0,0,0,(APTR)ID_MENU_Time},
  111. {NM_ITEM,NM_BARLABEL},
  112. {NM_ITEM,"T\0Show thinking",0,CHECKIT|CHECKED,0,(APTR)ID_MENU_ShowThinking},
  113. {NM_ITEM,"Null moves",0,CHECKIT|CHECKED,0,(APTR)ID_MENU_NullMove},
  114. {NM_TITLE,"Display"},
  115. {NM_ITEM,"Skins",0,0,0,(APTR)ID_MENU_Skin},
  116. {NM_ITEM,NM_BARLABEL},
  117. {NM_ITEM,"Reverse board",0,CHECKIT,0,(APTR)ID_MENU_ReverseBoard},
  118. {-1}};
  119.  
  120. static ULONG playcol[][3]={0xffffffff,0xffffffff,0xffffffff,0,0,0};
  121.  
  122. static ULONG mChessNew(struct IClass *cl,Object *obj,struct opSet *msg)
  123. {
  124. Object *win,*col,*mymove,*think,*board,*lv_white,*lv_black;
  125. Object *timewin,*timem,*timet,*times;
  126. obj=DoSuperNew(cl,obj,
  127.     MUIA_Application_Title,"AmiChess",
  128.     MUIA_Application_Base,"AmiChess",
  129.     MUIA_Application_Author,"Achim Stegemann",
  130.     MUIA_Application_Copyright,"Based on GnuChess 5.0.4",
  131.     MUIA_Application_Version,"$VER: AmiChess 0.2 (31.10.2002)",
  132.     MUIA_Application_Description,"Amiga port of GNUChess 5.0.4",
  133.     MUIA_Application_Menustrip,mui_menu=MUI_MakeObject(MUIO_MenustripNM,nm,MUIO_MenustripNM_CommandKeyCheck),
  134.     SubWindow,win=WindowObject,
  135.         MUIA_Window_Title,"AmiChess 0.2 by Achim Stegemann",
  136.         MUIA_Window_Screen,wbscreen,
  137.         WindowContents,VGroup,
  138.             Child,VGroup,
  139.                 GroupFrame,
  140.                 MUIA_Background,MUII_GroupBack,
  141.                 Child,HGroup,
  142.                     Child,col=ColorfieldObject,
  143.                         TextFrame,
  144.                         MUIA_FixWidthTxt,"W",
  145.                         MUIA_FixHeightTxt,"W",
  146.                         MUIA_Colorfield_RGB,playcol[0],
  147.                     End,
  148.                     Child,Label1("My move"),
  149.                     Child,mymove=TextObject,
  150.                         TextFrame,
  151.                         MUIA_Font,MUIV_Font_Fixed,
  152.                         MUIA_Background,MUII_TextBack,
  153.                         MUIA_Text_PreParse,MUIX_PH,
  154.                     End,
  155.                 End,
  156.                 Child,HGroup,
  157.                     Child,Label1("Best line"),
  158.                     Child,think=TextObject,
  159.                         TextFrame,
  160.                         MUIA_Font,MUIV_Font_Fixed,
  161.                         MUIA_Background,MUII_TextBack,
  162.                         MUIA_Text_PreParse,MUIX_PH,
  163.                     End,
  164.                 End,
  165.             End,
  166.             Child,HGroup,
  167.                 GroupFrame,
  168.                 MUIA_Background,MUII_GroupBack,
  169.                 Child,board=ColGroup(9),
  170.                     MUIA_Group_Spacing,0,
  171.                 End,
  172.                 Child,lv_white=ListviewObject,
  173.                     ReadListFrame,
  174.                     MUIA_Listview_Input,0,
  175.                     MUIA_Listview_List,ListObject,
  176.                         MUIA_Font,MUIV_Font_Fixed,
  177.                         MUIA_List_ConstructHook,MUIV_List_ConstructHook_String,
  178.                         MUIA_List_DestructHook,MUIV_List_DestructHook_String,
  179.                         MUIA_List_Title,"White",
  180.                     End,
  181.                 End,
  182.                 Child,lv_black=ListviewObject,
  183.                     ReadListFrame,
  184.                     MUIA_Listview_Input,0,
  185.                     MUIA_Listview_List,ListObject,
  186.                         MUIA_Font,MUIV_Font_Fixed,
  187.                         MUIA_List_ConstructHook,MUIV_List_ConstructHook_String,
  188.                         MUIA_List_DestructHook,MUIV_List_DestructHook_String,
  189.                         MUIA_List_Title,"Black",
  190.                     End,
  191.                 End,
  192.             End,
  193.         End,
  194.     End,
  195.     SubWindow,timewin=WindowObject,
  196.         MUIA_Window_Title,"Time settings",
  197.         MUIA_Window_Screen,wbscreen,
  198.         WindowContents,VGroup,
  199.             Child,HGroup,
  200.                 Child,timem=StringObject,
  201.                     StringFrame,
  202.                     MUIA_CycleChain,1,
  203.                     MUIA_String_Accept,"0123456789",
  204.                     MUIA_String_MaxLen,4,
  205.                     MUIA_String_Integer,0,
  206.                 End,
  207.                 Child,Label2("moves in"),
  208.                 Child,timet=StringObject,
  209.                     StringFrame,
  210.                     MUIA_CycleChain,1,
  211.                     MUIA_String_Accept,"0123456789",
  212.                     MUIA_String_MaxLen,4,
  213.                     MUIA_String_Integer,0,
  214.                 End,
  215.                 Child,Label2("minutes."),
  216.             End,
  217.             Child,Label2("Searchtime in sec"),
  218.             Child,times=StringObject,
  219.                 StringFrame,
  220.                 MUIA_CycleChain,1,
  221.                 MUIA_String_Accept,"0123456789",
  222.                 MUIA_String_MaxLen,4,
  223.                 MUIA_String_Integer,0,
  224.             End,
  225.         End,
  226.     End,
  227. End;
  228. if(obj)
  229.     {
  230.     ULONG i;
  231.     struct Data *data=(struct Data *)INST_DATA(cl,obj);
  232.     struct FileInfoBlock *fib;
  233.     BPTR lock;
  234.     data->win=win;
  235.     data->col=col;
  236.     data->mymove=mymove;
  237.     data->think=think;
  238.     data->lv_white=lv_white;
  239.     data->lv_black=lv_black;
  240.     data->board=board;
  241.     data->timewin=timewin;
  242.     data->timem=timem;
  243.     data->timet=timet;
  244.     data->times=times;
  245.     DoMethod(find(ID_MENU_New),MUIM_Notify,MUIA_Menuitem_Trigger,MUIV_EveryTime,MUIV_Notify_Application,1,MUIM_Chess_New);
  246.     DoMethod(find(ID_MENU_OpenEPD),MUIM_Notify,MUIA_Menuitem_Trigger,MUIV_EveryTime,MUIV_Notify_Application,1,MUIM_Chess_OpenEPD);
  247.     DoMethod(find(ID_MENU_OpenPGN),MUIM_Notify,MUIA_Menuitem_Trigger,MUIV_EveryTime,MUIV_Notify_Application,1,MUIM_Chess_OpenPGN);
  248.     DoMethod(find(ID_MENU_SaveEPD),MUIM_Notify,MUIA_Menuitem_Trigger,MUIV_EveryTime,MUIV_Notify_Application,1,MUIM_Chess_SaveEPD);
  249.     DoMethod(find(ID_MENU_SavePGN),MUIM_Notify,MUIA_Menuitem_Trigger,MUIV_EveryTime,MUIV_Notify_Application,1,MUIM_Chess_SavePGN);
  250.     DoMethod(find(ID_MENU_Autoplay),MUIM_Notify,MUIA_Menuitem_Checked,MUIV_EveryTime,MUIV_Notify_Application,2,MUIM_Chess_Autoplay,MUIV_TriggerValue);
  251.     DoMethod(find(ID_MENU_Supervisor),MUIM_Notify,MUIA_Menuitem_Checked,MUIV_EveryTime,MUIV_Notify_Application,2,MUIM_Chess_Supervisor,MUIV_TriggerValue);
  252.     DoMethod(find(ID_MENU_Swap),MUIM_Notify,MUIA_Menuitem_Trigger,MUIV_EveryTime,MUIV_Notify_Application,1,MUIM_Chess_SwapSides);
  253.     DoMethod(find(ID_MENU_Switch),MUIM_Notify,MUIA_Menuitem_Trigger,MUIV_EveryTime,MUIV_Notify_Application,1,MUIM_Chess_SwitchSides);
  254.     DoMethod(find(ID_MENU_Hint),MUIM_Notify,MUIA_Menuitem_Trigger,MUIV_EveryTime,MUIV_Notify_Application,1,MUIM_Chess_Hint);
  255.     DoMethod(find(ID_MENU_Undo),MUIM_Notify,MUIA_Menuitem_Trigger,MUIV_EveryTime,MUIV_Notify_Application,1,MUIM_Chess_Undo);
  256.     DoMethod(find(ID_MENU_Remove),MUIM_Notify,MUIA_Menuitem_Trigger,MUIV_EveryTime,MUIV_Notify_Application,1,MUIM_Chess_Remove);
  257.     DoMethod(find(ID_MENU_ShowThinking),MUIM_Notify,MUIA_Menuitem_Checked,MUIV_EveryTime,MUIV_Notify_Application,2,MUIM_Chess_Post,MUIV_TriggerValue);
  258.     DoMethod(find(ID_MENU_NullMove),MUIM_Notify,MUIA_Menuitem_Checked,MUIV_EveryTime,MUIV_Notify_Application,2,MUIM_Chess_NullMove,MUIV_TriggerValue);
  259.     DoMethod(find(ID_MENU_BookAdd),MUIM_Notify,MUIA_Menuitem_Trigger,MUIV_EveryTime,MUIV_Notify_Application,1,MUIM_Chess_BookAdd);
  260.     DoMethod(find(ID_MENU_BookOff),MUIM_Notify,MUIA_Menuitem_Checked,MUIV_EveryTime,MUIV_Notify_Application,1,MUIM_Chess_BookOff);
  261.     DoMethod(find(ID_MENU_BookOn),MUIM_Notify,MUIA_Menuitem_Checked,MUIV_EveryTime,MUIV_Notify_Application,1,MUIM_Chess_BookOn);
  262.     DoMethod(find(ID_MENU_BookBest),MUIM_Notify,MUIA_Menuitem_Checked,MUIV_EveryTime,MUIV_Notify_Application,1,MUIM_Chess_BookBest);
  263.     DoMethod(find(ID_MENU_BookWorst),MUIM_Notify,MUIA_Menuitem_Checked,MUIV_EveryTime,MUIV_Notify_Application,1,MUIM_Chess_BookWorst);
  264.     DoMethod(find(ID_MENU_BookRandom),MUIM_Notify,MUIA_Menuitem_Checked,MUIV_EveryTime,MUIV_Notify_Application,1,MUIM_Chess_BookRandom);
  265.     DoMethod(find(ID_MENU_Depth0),MUIM_Notify,MUIA_Menuitem_Checked,MUIV_EveryTime,MUIV_Notify_Application,2,MUIM_Chess_Depth,0);
  266.     DoMethod(find(ID_MENU_Depth1),MUIM_Notify,MUIA_Menuitem_Checked,MUIV_EveryTime,MUIV_Notify_Application,2,MUIM_Chess_Depth,1);
  267.     DoMethod(find(ID_MENU_Depth2),MUIM_Notify,MUIA_Menuitem_Checked,MUIV_EveryTime,MUIV_Notify_Application,2,MUIM_Chess_Depth,2);
  268.     DoMethod(find(ID_MENU_Depth3),MUIM_Notify,MUIA_Menuitem_Checked,MUIV_EveryTime,MUIV_Notify_Application,2,MUIM_Chess_Depth,3);
  269.     DoMethod(find(ID_MENU_Depth4),MUIM_Notify,MUIA_Menuitem_Checked,MUIV_EveryTime,MUIV_Notify_Application,2,MUIM_Chess_Depth,4);
  270.     DoMethod(find(ID_MENU_Depth5),MUIM_Notify,MUIA_Menuitem_Checked,MUIV_EveryTime,MUIV_Notify_Application,2,MUIM_Chess_Depth,5);
  271.     DoMethod(find(ID_MENU_Depth6),MUIM_Notify,MUIA_Menuitem_Checked,MUIV_EveryTime,MUIV_Notify_Application,2,MUIM_Chess_Depth,6);
  272.     DoMethod(find(ID_MENU_Depth7),MUIM_Notify,MUIA_Menuitem_Checked,MUIV_EveryTime,MUIV_Notify_Application,2,MUIM_Chess_Depth,7);
  273.     DoMethod(find(ID_MENU_Depth8),MUIM_Notify,MUIA_Menuitem_Checked,MUIV_EveryTime,MUIV_Notify_Application,2,MUIM_Chess_Depth,8);
  274.     DoMethod(find(ID_MENU_Depth9),MUIM_Notify,MUIA_Menuitem_Checked,MUIV_EveryTime,MUIV_Notify_Application,2,MUIM_Chess_Depth,9);
  275.     DoMethod(find(ID_MENU_Depth10),MUIM_Notify,MUIA_Menuitem_Checked,MUIV_EveryTime,MUIV_Notify_Application,2,MUIM_Chess_Depth,10);
  276.     DoMethod(find(ID_MENU_Time),MUIM_Notify,MUIA_Menuitem_Trigger,MUIV_EveryTime,timewin,3,MUIM_Set,MUIA_Window_Open,1);
  277.     DoMethod(find(ID_MENU_ReverseBoard),MUIM_Notify,MUIA_Menuitem_Checked,MUIV_EveryTime,MUIV_Notify_Application,2,MUIM_Chess_ReverseBoard,MUIV_TriggerValue);
  278.     DoMethod(win,MUIM_Notify,MUIA_Window_CloseRequest,1,MUIV_Notify_Application,2,MUIM_Application_ReturnID,MUIV_Application_ReturnID_Quit);
  279.     DoMethod(timewin,MUIM_Notify,MUIA_Window_CloseRequest,1,MUIV_Notify_Self,3,MUIM_Set,MUIA_Window_Open,0);
  280.     DoMethod(timem,MUIM_Notify,MUIA_String_Acknowledge,MUIV_EveryTime,MUIV_Notify_Application,2,MUIM_Chess_Time,0);
  281.     DoMethod(timet,MUIM_Notify,MUIA_String_Acknowledge,MUIV_EveryTime,MUIV_Notify_Application,2,MUIM_Chess_Time,1);
  282.     DoMethod(times,MUIM_Notify,MUIA_String_Acknowledge,MUIV_EveryTime,MUIV_Notify_Application,2,MUIM_Chess_Time,2);
  283.     data->filereq=MUI_AllocAslRequestTags(ASL_FileRequest,ASLFR_RejectIcons,TAG_END);
  284.     for(i=0;i<64;i++)
  285.         {
  286.         if(i%8==0) DoMethod(board,OM_ADDMEMBER,data->text1[i/8]=TextObject,MUIA_FixWidthTxt,"W",MUIA_Font,MUIV_Font_Big,MUIA_Text_PreParse,MUIX_B,End);
  287.         DoMethod(board,OM_ADDMEMBER,data->field[i]=NewObject(MUI_Field_Class->mcc_Class,0,MUIA_UserData,i,TAG_END));
  288.         }
  289.     DoMethod(board,OM_ADDMEMBER,HVSpace);
  290.     for(i=0;i<8;i++) DoMethod(board,OM_ADDMEMBER,data->texta[i]=TextObject,MUIA_Font,MUIV_Font_Big,MUIA_Text_PreParse,"\033c\033b",MUIA_FixWidthTxt,"W",End);
  291.     if(fib=AllocDosObject(DOS_FIB,0))
  292.         {
  293.         if(lock=Lock(skin_folder,SHARED_LOCK))
  294.             {
  295.             if(Examine(lock,fib)!=DOSFALSE)
  296.                 {
  297.                 Object *menu=find(ID_MENU_Skin);
  298.                 i=0;
  299.                 while(ExNext(lock,fib)!=DOSFALSE)
  300.                     {
  301.                     Object *sub;
  302.                     char *str;
  303.                     str=malloc(strlen(fib->fib_FileName)+1);
  304.                     strcpy(str,fib->fib_FileName);
  305.                     sub=MenuitemObject,
  306.                         MUIA_Menuitem_Checkit,1,
  307.                         MUIA_Menuitem_Checked,strcmp(fib->fib_FileName,"Original")?0:1,
  308.                         MUIA_Menuitem_Title,str,
  309.                         MUIA_Menuitem_Exclude,~(1<<i),
  310.                     End;
  311.                     DoMethod(menu,OM_ADDMEMBER,sub);
  312.                     DoMethod(sub,MUIM_Notify,MUIA_Menuitem_Checked,MUIV_EveryTime,MUIV_Notify_Application,2,MUIM_Chess_Skin,sub);
  313.                     i++;
  314.                     }
  315.                 }
  316.             UnLock(lock);
  317.             }
  318.         FreeDosObject(DOS_FIB,fib);
  319.         }
  320.     }
  321. return(ULONG)obj;
  322. }
  323.  
  324. static ULONG mChessDispose(struct IClass *cl,Object *obj,Msg msg)
  325. {
  326. struct Data *data=(struct Data *)INST_DATA(cl,obj);
  327. if(data->filereq) MUI_FreeAslRequest(data->filereq);
  328. return DoSuperMethodA(cl,obj,msg);
  329. }
  330.  
  331. static void mChessNewgame(struct IClass *cl,Object *obj)
  332. {
  333. struct Data *data=(struct Data *)INST_DATA(cl,obj);
  334. InitVars();
  335. NewPosition();
  336. myrating=opprating=0;
  337. DoMethod(obj,MUIM_Chess_ShowThinking,0);
  338. DoMethod(obj,MUIM_Chess_MyMove,0);
  339. SetAttrs(find(ID_MENU_Autoplay),MUIA_Menuitem_Checked,0,TAG_END);
  340. SetAttrs(find(ID_MENU_Supervisor),MUIA_Menuitem_Checked,0,TAG_END);
  341. DoMethod(obj,MUIM_Chess_ClearList);
  342. DoMethod(obj,MUIM_Chess_ShowBoard);
  343. }
  344.  
  345. static void mChessOpenEPD(struct IClass *cl,Object *obj)
  346. {
  347. struct Data *data=(struct Data *)INST_DATA(cl,obj);
  348. if(MUI_AslRequestTags(data->filereq,ASLFR_InitialPattern,"#?",ASLFR_DoSaveMode,0,TAG_END))
  349.     {
  350.     BPTR lock;
  351.     if(lock=Lock(data->filereq->fr_Drawer,SHARED_LOCK))
  352.         {
  353.         BPTR olddir=CurrentDir(lock);
  354.         DoMethod(obj,MUIM_Chess_ClearList);
  355.         LoadEPD(data->filereq->fr_File);
  356.         if(!ValidateBoard())
  357.             {
  358.             SET(flags,ENDED);
  359.             DoMethod(obj,MUIM_Chess_ShowThinking,"Board is wrong!");
  360.             }
  361.         CurrentDir(olddir);
  362.         UnLock(lock);
  363.         }
  364.     }
  365. }
  366.  
  367. static void mChessOpenPGN(struct IClass *cl,Object *obj)
  368. {
  369. struct Data *data=(struct Data *)INST_DATA(cl,obj);
  370. if(MUI_AslRequestTags(data->filereq,ASLFR_InitialPattern,"#?.pgn",ASLFR_DoSaveMode,0,TAG_END))
  371.     {
  372.     BPTR lock;
  373.     if(lock=Lock(data->filereq->fr_Drawer,SHARED_LOCK))
  374.         {
  375.         BPTR olddir=CurrentDir(lock);
  376.         PGNReadFromFile(data->filereq->fr_File);
  377.         CurrentDir(olddir);
  378.         UnLock(lock);
  379.         }
  380.     }
  381. }
  382.  
  383. static void mChessSaveEPD(struct IClass *cl,Object *obj)
  384. {
  385. struct Data *data=(struct Data *)INST_DATA(cl,obj);
  386. if(MUI_AslRequestTags(data->filereq,ASLFR_InitialPattern,"#?",ASLFR_DoSaveMode,1,TAG_END))
  387.     {
  388.     BPTR lock;
  389.     if(lock=Lock(data->filereq->fr_Drawer,SHARED_LOCK))
  390.         {
  391.         BPTR olddir=CurrentDir(lock);
  392.         SaveEPD(data->filereq->fr_File);
  393.         CurrentDir(olddir);
  394.         UnLock(lock);
  395.         }
  396.     }
  397. }
  398.  
  399. static void mChessSavePGN(struct IClass *cl,Object *obj)
  400. {
  401. struct Data *data=(struct Data *)INST_DATA(cl,obj);
  402. if(MUI_AslRequestTags(data->filereq,ASLFR_InitialPattern,"#?.pgn",ASLFR_DoSaveMode,1,TAG_END))
  403.     {
  404.     BPTR lock;
  405.     if(lock=Lock(data->filereq->fr_Drawer,SHARED_LOCK))
  406.         {
  407.         BPTR olddir=CurrentDir(lock);
  408.         PGNSaveToFile(data->filereq->fr_File,"");
  409.         CurrentDir(olddir);
  410.         UnLock(lock);
  411.         }
  412.     }
  413. }
  414.  
  415. static void mChessAutoplay(struct MUIP_Chess_Autoplay *msg)
  416. {
  417. if(msg->autoplay) SET(flags,AUTOPLAY);
  418. else CLEAR(flags,AUTOPLAY);
  419. }
  420.  
  421. static void mChessSwapSides(Object *obj)
  422. {
  423. if(flags&SUPERVISOR) DoMethod(obj,MUIM_Chess_ShowThinking,"Not possible in supervisor mode.");
  424. else
  425.     {
  426.     CLEAR(flags,TIMEOUT);
  427.     CLEAR(flags,ENDED);
  428.     computer=board.side;
  429.     //SetAttrs(obj,MUIA_Application_Sleep,1,TAG_END);
  430.     Iterate();
  431.     if(flags&ENDED)
  432.         {
  433.         SetAttrs(find(ID_MENU_Autoplay),MUIA_NoNotify,1,MUIA_Menuitem_Checked,0,TAG_END);
  434.         CLEAR(flags,AUTOPLAY);
  435.         }
  436.     DisplayBeep(0);
  437.     //SetAttrs(obj,MUIA_Application_Sleep,0,TAG_END);
  438.     }
  439. }
  440.  
  441. static void mChessSwitchSides(Object *obj)
  442. {
  443. board.side=1^board.side;
  444. DoMethod(obj,MUIM_Chess_Side);
  445. }
  446.  
  447. static void mChessHint(struct IClass *cl,Object *obj)
  448. {
  449. struct Data *data=(struct Data *)INST_DATA(cl,obj);
  450. int pvar[2];
  451. if((flags&USEHASH))
  452.     {
  453.     if(TTGetPV(board.side,2,rootscore,&pvar[2]))
  454.         {
  455.         GenMoves(2); 
  456.         SANMove(pvar[2],2);
  457.         SetAttrs(data->mymove,MUIA_Text_Contents,SANmv,TAG_END);
  458.         }
  459.     }
  460. }
  461.  
  462. static void mChessUndo(struct IClass *cl,Object *obj)
  463. {
  464. struct Data *data=(struct Data *)INST_DATA(cl,obj);
  465. if(GameCnt>=0) UnmakeMove(board.side,&Game[GameCnt].move);
  466. else DisplayBeep(0);
  467. MoveLimit[board.side]++;
  468. TimeLimit[board.side]+=Game[GameCnt+1].et;
  469. DoMethod(board.side==white?data->lv_white:data->lv_black,MUIM_List_Remove,MUIV_List_Remove_Last);
  470. DoMethod(obj,MUIM_Chess_ShowThinking,0);
  471. DoMethod(obj,MUIM_Chess_Side);
  472. DoMethod(obj,MUIM_Chess_ShowBoard);
  473. }
  474.  
  475. static void mChessRemove(struct IClass *cl,Object *obj)
  476. {
  477. struct Data *data=(struct Data *)INST_DATA(cl,obj);
  478. if(GameCnt>=0)
  479.     {
  480.     CLEAR(flags,ENDED);
  481.     CLEAR(flags,TIMEOUT);
  482.     DoMethod(board.side==white?data->lv_white:data->lv_black,MUIM_List_Remove,MUIV_List_Remove_Last);
  483.     UnmakeMove(board.side,&Game[GameCnt].move);
  484.     if(GameCnt>=0)
  485.         {
  486.         DoMethod(board.side==white?data->lv_white:data->lv_black,MUIM_List_Remove,MUIV_List_Remove_Last);
  487.         UnmakeMove(board.side,&Game[GameCnt].move);
  488.         DoMethod(obj,MUIM_Chess_ShowThinking,0);
  489.         DoMethod(obj,MUIM_Chess_ShowBoard);
  490.         }
  491.     DoMethod(obj,MUIM_Chess_Side);
  492.     PGNSaveToFile("log","");
  493.     }
  494. else DisplayBeep(0);
  495. }
  496.  
  497. static void mChessSupervisor(struct MUIP_Chess_Supervisor *msg)
  498. {
  499. if(msg->value) SET(flags,SUPERVISOR);
  500. else CLEAR(flags,SUPERVISOR);
  501. }
  502.  
  503. static void mChessPost(Object *obj,struct MUIP_Chess_Post *msg)
  504. {
  505. if(msg->value) SET(flags,POST);
  506. else
  507.     {
  508.     CLEAR(flags,POST);
  509.     DoMethod(obj,MUIM_Chess_ShowThinking,0);
  510.     }
  511. }
  512.  
  513. static void mChessBookAdd(struct IClass *cl,Object *obj)
  514. {
  515. struct Data *data=(struct Data *)INST_DATA(cl,obj);
  516. if(MUI_AslRequestTags(data->filereq,ASLFR_InitialPattern,"#?.pgn",ASLFR_DoSaveMode,0,TAG_END))
  517.     {
  518.     BPTR lock;
  519.     if(lock=Lock(data->filereq->fr_Drawer,SHARED_LOCK))
  520.         {
  521.         BPTR olddir=CurrentDir(lock);
  522.         BookPGNReadFromFile(data->filereq->fr_File);
  523.         CurrentDir(olddir);
  524.         UnLock(lock);
  525.         }
  526.     }
  527. }
  528.  
  529. static void mChessBookOn()
  530. {
  531. bookmode=BOOKPREFER;
  532. }
  533.  
  534. static void mChessBookOff()
  535. {
  536. bookmode=BOOKOFF;
  537. }
  538.  
  539. static void mChessBookBest()
  540. {
  541. bookmode=BOOKBEST;
  542. }
  543.  
  544. static void mChessBookWorst()
  545. {
  546. bookmode=BOOKWORST;
  547. }
  548.  
  549. static void mChessBookRandom()
  550. {
  551. bookmode=BOOKRAND;
  552. }
  553.  
  554. static void mChessDepth(struct MUIP_Chess_Depth *msg)
  555. {
  556. SearchDepth=msg->depth;
  557. }
  558.  
  559. static void mChessTime(struct IClass *cl,Object *obj,struct MUIP_Chess_Time *msg)
  560. {
  561. struct Data *data=(struct Data *)INST_DATA(cl,obj);
  562. ULONG val;
  563. char text[50];
  564. switch(msg->flag)
  565.     {
  566.     case 0:
  567.         GetAttr(MUIA_String_Integer,data->timem,&val);
  568.         TCMove=val;
  569.         break;
  570.     case 1:
  571.         GetAttr(MUIA_String_Integer,data->timet,&val);
  572.         TCTime=val;
  573.         break;
  574.     default:
  575.         GetAttr(MUIA_String_Integer,data->times,&val);
  576.         if(!val) val=5;
  577.         SearchTime=val;
  578.     }
  579. if(!TCMove&&!TCTime)
  580.     {
  581. /*    SearchTime=5; */
  582.     sprintf(text,"Searchtime set to %.1f secs.",SearchTime);
  583.     DoMethod(obj,MUIM_Chess_ShowThinking,text);
  584.     CLEAR(flags,TIMECTL);
  585.     }
  586. else
  587.     {
  588.     if(!TCMove)
  589.         {
  590.         TCMove=35;
  591.     /*    printf("TCMove=%d\n",TCMove); */
  592.         suddendeath=1;
  593.         }
  594.     else suddendeath=0;
  595.     if(!TCTime)
  596.         {
  597.         SET(flags,TIMECTL);
  598.         SearchTime=TCinc/2;
  599.     /*    printf("Fischer increment of %d seconds\n",TCinc); */
  600.         }
  601.     else
  602.         {
  603.         SET(flags,TIMECTL);
  604.     /*    MoveLimit[white]=MoveLimit[black]=TCMove-(GameCnt+1)/2; */
  605.         MoveLimit[white]=MoveLimit[black]=TCMove;
  606.         TimeLimit[white]=TimeLimit[black]=TCTime*60;
  607.     /*
  608.         printf("Time Control: %d moves in %.1f secs\n",MoveLimit[white],TimeLimit[white]);
  609.         printf("Fischer increment of %d seconds\n",TCinc);
  610.     */
  611.         }
  612.     sprintf(text,"Time control:%d moves in %.1f secs",MoveLimit[white],TimeLimit[white]);
  613.     DoMethod(obj,MUIM_Chess_ShowThinking,text);
  614.     }
  615. }
  616.  
  617. static void mChessWinOpen(struct IClass *cl,Object *obj)
  618. {
  619. struct Data *data=(struct Data *)INST_DATA(cl,obj);
  620. SetAttrs(data->win,MUIA_Window_Open,1,TAG_END);
  621. }
  622.  
  623. static void    mChessShowBoard(struct IClass *cl,Object *obj)
  624. {
  625. struct Data *data=(struct Data *)INST_DATA(cl,obj);
  626. char t[2];
  627. int i;
  628. t[1]=0;
  629. for(i=0;i<8;i++)
  630.     {
  631.     if(flags&REVERSEBOARD) t[0]='h'-i;
  632.     else t[0]='a'+i;
  633.     SetAttrs(data->texta[i],MUIA_Text_Contents,t,TAG_END);
  634.     if(flags&REVERSEBOARD) t[0]='1'+i;
  635.     else t[0]='8'-i;
  636.     SetAttrs(data->text1[i],MUIA_Text_Contents,t,TAG_END);
  637.     }
  638. MUI_Redraw(data->board,MADF_DRAWOBJECT);
  639. }
  640.  
  641. static void    mChessShowThinking(struct IClass *cl,Object *obj,struct MUIP_Chess_ShowThinking *msg)
  642. {
  643. struct Data *data=(struct Data *)INST_DATA(cl,obj);
  644. SetAttrs(data->think,MUIA_Text_Contents,msg->line,TAG_END);
  645. }
  646.  
  647. static void mChessNullMove(struct MUIP_Chess_NullMove *msg)
  648. {
  649. if(msg->value) SET(flags,USENULL);
  650. else CLEAR(flags,USENULL);
  651. }
  652.  
  653. static void mChessReverseBoard(Object *obj,struct MUIP_Chess_ReverseBoard *msg)
  654. {
  655. if(msg->reverse) SET(flags,REVERSEBOARD);
  656. else CLEAR(flags,REVERSEBOARD);
  657. DoMethod(obj,MUIM_Chess_ShowBoard);
  658. }
  659.  
  660. static void mChessMyMove(struct IClass *cl,Object *obj,struct MUIP_Chess_MyMove *msg)
  661. {
  662. struct Data *data=(struct Data *)INST_DATA(cl,obj);
  663. SetAttrs(data->mymove,MUIA_Text_Contents,msg->move,TAG_END);
  664. }
  665.  
  666. static void mChessSide(struct IClass *cl,Object *obj)
  667. {
  668. struct Data *data=(struct Data *)INST_DATA(cl,obj);
  669. SetAttrs(data->col,MUIA_Colorfield_RGB,playcol[board.side],TAG_END);
  670. }
  671.  
  672. static void mChessSkin(Object *obj,struct MUIP_Chess_Skin *msg)
  673. {
  674. char *name;
  675. GetAttr(MUIA_Menuitem_Title,msg->menu,&name);
  676. LoadSkin(name);
  677. DoMethod(obj,MUIM_Chess_ShowBoard);
  678. }
  679.  
  680. static void mChessClearFlag(struct MUIP_Chess_ClearFlag *msg)
  681. {
  682. if(msg->flag==AUTOPLAY)
  683.     {
  684.     SetAttrs(find(ID_MENU_Autoplay),MUIA_NoNotify,1,MUIA_Menuitem_Checked,0,TAG_END);
  685.     CLEAR(flags,AUTOPLAY);
  686.     }
  687. }
  688.  
  689. static void mChessAddMove(struct IClass *cl,Object *obj,struct MUIP_Chess_AddMove *msg)
  690. {
  691. struct Data *data=(struct Data *)INST_DATA(cl,obj);
  692. DoMethod(msg->side==white?data->lv_white:data->lv_black,MUIM_List_InsertSingle,msg->move,MUIV_List_Insert_Bottom);
  693. }
  694.  
  695. static void mChessClearList(struct IClass *cl,Object *obj)
  696. {
  697. struct Data *data=(struct Data *)INST_DATA(cl,obj);
  698. DoMethod(data->lv_white,MUIM_List_Clear);
  699. DoMethod(data->lv_black,MUIM_List_Clear);
  700. }
  701.  
  702. #ifdef __GNUC__
  703. static ULONG Dispatcher(struct IClass *cl __asm("a0"),Object *obj  __asm("a2"),Msg msg  __asm("a1"))
  704. #elif defined __VBCC__
  705. static ULONG Dispatcher(__reg("a0") struct IClass *cl,__reg("a2") Object *obj,__reg("a1") Msg msg)
  706. #else
  707. static ULONG Dispatcher(register __a0 struct IClass *cl,register __a2 Object *obj,register __a1 Msg msg)
  708. #endif
  709. {
  710. ULONG retval;
  711. switch(msg->MethodID)
  712.     {
  713.     case OM_NEW:
  714.         retval=mChessNew(cl,obj,(struct opSet *)msg);
  715.         break;
  716.     case OM_DISPOSE:
  717.         retval=mChessDispose(cl,obj,msg);
  718.         break;
  719.     case MUIM_Chess_WinOpen:
  720.         mChessWinOpen(cl,obj);
  721.         break;
  722.     case MUIM_Chess_New:
  723.         mChessNewgame(cl,obj);
  724.         break;
  725.     case MUIM_Chess_OpenEPD:
  726.         mChessOpenEPD(cl,obj);
  727.         break;
  728.     case MUIM_Chess_OpenPGN:
  729.         mChessOpenPGN(cl,obj);
  730.         break;
  731.     case MUIM_Chess_SaveEPD:
  732.         mChessSaveEPD(cl,obj);
  733.         break;
  734.     case MUIM_Chess_SavePGN:
  735.         mChessSavePGN(cl,obj);
  736.         break;
  737.     case MUIM_Chess_Autoplay:
  738.         mChessAutoplay((struct MUIP_Chess_Autoplay *)msg);
  739.         break;
  740.     case MUIM_Chess_SwapSides:
  741.         mChessSwapSides(obj);
  742.         break;
  743.     case MUIM_Chess_SwitchSides:
  744.         mChessSwitchSides(obj);
  745.         break;
  746.     case MUIM_Chess_Hint:
  747.         mChessHint(cl,obj);
  748.         break;
  749.     case MUIM_Chess_Undo:
  750.         mChessUndo(cl,obj);
  751.         break;
  752.     case MUIM_Chess_Remove:
  753.         mChessRemove(cl,obj);
  754.         break;
  755.     case MUIM_Chess_Supervisor:
  756.         mChessSupervisor((struct MUIP_Chess_Supervisor *)msg);
  757.         break;
  758.     case MUIM_Chess_Post:
  759.         mChessPost(obj,(struct MUIP_Chess_Post *)msg);
  760.         break;
  761.     case MUIM_Chess_BookAdd:
  762.         mChessBookAdd(cl,obj);
  763.         break;
  764.     case MUIM_Chess_BookOn:
  765.         mChessBookOn();
  766.         break;
  767.     case MUIM_Chess_BookOff:
  768.         mChessBookOff();
  769.         break;
  770.     case MUIM_Chess_BookBest:
  771.         mChessBookBest();
  772.         break;
  773.     case MUIM_Chess_BookWorst:
  774.         mChessBookWorst();
  775.         break;
  776.     case MUIM_Chess_BookRandom:
  777.         mChessBookRandom();
  778.         break;
  779.     case MUIM_Chess_Depth:
  780.         mChessDepth((struct MUIP_Chess_Depth *)msg);
  781.         break;
  782.     case MUIM_Chess_Time:
  783.         mChessTime(cl,obj,(struct MUIP_Chess_Time *)msg);
  784.         break;
  785.     case MUIM_Chess_ShowBoard:
  786.         mChessShowBoard(cl,obj);
  787.         break;
  788.     case MUIM_Chess_ShowThinking:
  789.         mChessShowThinking(cl,obj,(struct MUIP_Chess_ShowThinking *)msg);
  790.         break;
  791.     case MUIM_Chess_NullMove:
  792.         mChessNullMove((struct MUIP_Chess_NullMove *)msg);
  793.         break;
  794.     case MUIM_Chess_ReverseBoard:
  795.         mChessReverseBoard(obj,(struct MUIP_Chess_ReverseBoard *)msg);
  796.         break;
  797.     case MUIM_Chess_MyMove:
  798.         mChessMyMove(cl,obj,(struct MUIP_Chess_MyMove *)msg);
  799.         break;
  800.     case MUIM_Chess_Side:
  801.         mChessSide(cl,obj);
  802.         break;
  803.     case MUIM_Chess_Skin:
  804.         mChessSkin(obj,(struct MUIP_Chess_Skin *)msg);
  805.         break;
  806.     case MUIM_Chess_ClearFlag:
  807.         mChessClearFlag((struct MUIP_Chess_ClearFlag *)msg);
  808.         break;
  809.     case MUIM_Chess_AddMove:
  810.         mChessAddMove(cl,obj,(struct MUIP_Chess_AddMove *)msg);
  811.         break;
  812.     case MUIM_Chess_ClearList:
  813.         mChessClearList(cl,obj);
  814.         break;
  815.     default:
  816.         retval=DoSuperMethodA(cl,obj,msg);
  817.     }
  818. return retval;
  819. }
  820.  
  821. void INIT_6_MUI_Chess_Class()
  822. {
  823. if(!(MUI_Chess_Class=MUI_CreateCustomClass(0,MUIC_Application,0,sizeof(struct Data),(APTR)Dispatcher))) exit(20);
  824. }
  825.  
  826. void EXIT_6_MUI_Chess_Class()
  827. {
  828. if(MUI_Chess_Class) MUI_DeleteCustomClass(MUI_Chess_Class);
  829. }
  830.  
  831.